<?php
function refresh_to($url,$msg,$delay){
echo "<head>
<META http-equiv=Refresh Content='$delay; Url=$url'>
<link rel=\"stylesheet\" type=\"text/css\" href=\"themes/default/layout.css\">
</head>
<body>
<div id=\"messageBox\" align='center'>
<table width=\"100%\"><tr><th class='message_header'>Server Proses, wait ...</th></tr>
<tr><td valign='top'>$msg</td></tr>
</table></div>
<div id=\"loading\"><strong>loading...</strong><span></span></div>
</body>";
}
function error_msg($message){
echo "<div id=\"messageBox\">
<div class='error_header'> ERROR</div>
<table width=\"100%\">
<tr><td valign=top width='64' rowspan='2'><img class=\"icon_message\" src=\"images/erroricon.png\" /></td>
<td  valign='middle' align='center' class='error_message'>$message</td></tr>
    <tr><td align=center><a href=\"javascript:history.go(-1);\">
    <img src=\"images\back_button.png\" title=\"Back to Previous Page\"/></a></td></tr></table>
</td></tr></table>
</div>";

}
function message_box($title,$message){
echo "<div id=\"messageBox\" align='center'>
<table><tr><th class='message_header'>$title</th></tr>
<tr><td valign='top'>$message</td></tr></table></div>";

}

class database {
	var $counter;
	var $affected_rows_total;
	function set_host($set_host) {
	$this->host = $set_host;
	}

	function set_database($set_database) {
	$this->database = $set_database;
	}

	function set_username($set_username) {
	$this->username = $set_username;
	}

	function set_password($set_password) {
	$this->password = $set_password;
	}

	function connect() {
	

	$this->connect = @mysql_connect($this->host, $this->username, $this->password);
		if(!$this->connect) {
		error_msg("Failed connecting to database server\r\n\r\n" . mysql_error() . "");
		}

	return $this->connect;
	}

	function select_db() {
	global $error_handling;

	$this->select_db = @mysql_select_db($this->database, $this->connect);
		if(!$this->select_db) {
		error_msg("Failed selecting to database\r\n\r\n" . mysql_error() . "");
		}

	return $this->select_db;
	}

	function query($query) {
	global $error_handling;

	$this->result = mysql_query($query);
		if(!$this->result) {
		error_msg("Error Message: Failed executing database query\r\nDate/Time: " . date('Y-m-d H:i:s') . "\r\nQuery: $query\r\nMySQL Error: " . mysql_error() . "");
		}

	$detect1 = preg_replace('/DELETE/siU', 1, $query);
	$detect2 = preg_replace('/UPDATE/siU', 1, $query);
	$detect3 = preg_replace('/INSERT/siU', 1, $query);

		if($detect1 == 1 || $detect2 == 1 || $detect2 == 1) {
		$this->affected_rows = mysql_affected_rows();
		$this->affected_rows_total += $this->affected_rows;
		}

	$this->counter++;
	return $this->result;
	}

	function fetch_row($query) {
	$this->result = mysql_query($query);
	$this->row = $this->fetch_array($this->result);

	$this->counter++;
	return $this->row;
	}

	function fetch_array($query) {
	$this->row = @mysql_fetch_array($query);

	return $this->row;
	}

	function num_rows($query) {
	$this->result = mysql_query($query);
	$this->num_rows = mysql_num_rows($this->result);

	$this->counter++;
	return $this->num_rows;
	}

	function count_queries() {
	return $this->counter;
	}

	function affected_rows() {
		if($this->affected_rows_total == '0') {
		$this->affected_rows_total = '0';
		}
	function result($argIndex, $argRow=0, $argField=0)
	{
    return mysql_result($argIndex, $argRow, $argField);
	};
	return $this->affected_rows_total; 
	}
	function free_result($argIndex)
	{
    mysql_free_result($argIndex);
    return;
	}
	function close() {
	register_shutdown_function('mysql_close');
	}
	function db_error()
	{
    return mysql_error();
	}

	function data_seek($result,$argIndex)
	{
    return mysql_data_seek($result,$argIndex);
	}
}
$db=new database();
$db->set_host($db_host);
$db->set_database($db_name);
$db->set_username($db_user);
$db->set_password($db_pass);
$db->connect();
$db->select_db();
?>